package ${package.Controller};

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.springboot.util.AjaxResult;
import cn.hutool.core.util.StrUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.Date;

import ${package.Service}.${table.serviceName};
import ${package.Entity}.${entity};


/**
 * @描述：$!{table.comment}控制类
 * @作者: ${author}
 * @日期: ${date}
 */
@RestController
@RequestMapping("/${entity}")
@Api(tags = "API - ${table.controllerName}")
public class ${table.controllerName}{

    @Autowired
    public ${table.serviceName} ${table.entityPath}Service;

    /**
     * @描述: 根据实体属性分页查询
     * @入参:
     * @出参:
     * @作者: ${author}
     * @日期: ${date}
     **/
    @PostMapping("/listByEntity")
    @ApiOperation(value = " 根据实体属性分页查询")
    public AjaxResult listByEntity(Page page, ${entity} ${table.entityPath}){
        LambdaQueryWrapper<${entity}> wrapper=new QueryWrapper().lambda();
        IPage<${entity}> iPage= ${table.entityPath}Service.page(page,wrapper);
        return AjaxResult.success(iPage);
    }

    /**
     * @描述: 根据主键查询
     * @入参:
     * @出参:
     * @作者: ${author}
     * @日期: ${date}
     **/
    @PostMapping("/findById")
    @ApiOperation(value = "根据主键查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true,dataType = "string", paramType = "query")
    })
    public AjaxResult findById(String id){
        if(StrUtil.isBlank(id)){
            return new AjaxResult(AjaxResult.Type.ERROR,"id不能为空");
        }
        ${entity} ${table.entityPath} = ${table.entityPath}Service.getById(id);
        return AjaxResult.success(${table.entityPath});
    }

    /**
     * @描述: 新增
     * @入参:
     * @出参:
     * @作者: ${author}
     * @日期: ${date}
     **/
    @PostMapping("/save")
    @ApiOperation(value = "新增")
    public AjaxResult save(${entity} ${table.entityPath}){
        prepareData(${table.entityPath},true);
        ${table.entityPath}Service.save(${table.entityPath});
        return AjaxResult.success();
    }

    /**
     * @描述: 修改
     * @入参:
     * @出参:
     * @作者: ${author}
     * @日期: ${date}
     **/
    @PostMapping("/update")
    @ApiOperation(value = "修改")
    public AjaxResult update(${entity} ${table.entityPath}){
        prepareData(${table.entityPath},false);
        ${table.entityPath}Service.updateById(${table.entityPath});
        return AjaxResult.success();
    }

    /**
     * @描述: 根据主键删除
     * @入参: id
     * @出参:
     * @作者: ${author}
     * @日期: ${date}
     **/
    @PostMapping("/delete")
    @ApiOperation(value = "根据主键删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true,
                    dataType = "string", paramType = "query")
    })
    public AjaxResult delete(String id){
        ${table.entityPath}Service.removeById(id);
        return AjaxResult.success();
    }


    private void prepareData(${entity} ${table.entityPath},boolean isSave){
<#--        if(isSave){-->
<#--            ${table.entityPath}.setCreateTime(new Date());-->
<#--        }else{-->
<#--            ${table.entityPath}.updateDate(new Date());-->
<#--        }-->
    }

}